package com.liu.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liu.common.commons.PageVo;
import com.liu.service.dao.SysUserMapper;
import com.liu.service.entity.SysUser;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.service.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.poi.ss.formula.functions.T;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lxh
 * @since 2020-11-29
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper baseMapper;

    @Override
    public IPage<SysUser> findListByPage(PageVo< T> page){
           Page<SysUser> pageInfo = new Page<>(page.getCurrent(), page.getSize());
       QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        //todo:构造查询参数 根据业务添加

       if (StringUtils.isNotBlank(page.getSortField())){
           wrapper.orderBy(true,0!=page.getDirection() ,page.getSortField());
       }else {
           wrapper.orderByDesc("id");
       }
        IPage<SysUser> iPage = baseMapper.selectPage(pageInfo, wrapper);
        return iPage;
    }

    @Override
    public Boolean add(SysUser sysUser){
    sysUser.setId(IdWorker.getId());
        baseMapper.insert(sysUser);
        return true;
    }

    @Override
    public Boolean delete(Long id){
        baseMapper.deleteById(id);
        return true;
    }

    @Override
    public Boolean updateData(SysUser sysUser){
        baseMapper.updateById(sysUser);
        return true;
    }

    @Override
    public SysUser findById(Long id){
        SysUser sysUser = baseMapper.selectById(id);
        return sysUser;
    }

    @Override
    public SysUser getUserByUserName(String userName) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>().eq(StringUtils.isNotBlank(userName), "login_name", userName);
        SysUser sysUser = this.getOne(wrapper);
        return  sysUser;
    }
}
